package com.xmut.xmutbackend.service.impl;

import com.xmut.xmutbackend.entity.Emp;
import com.xmut.xmutbackend.entity.EmpStatistics;
import com.xmut.xmutbackend.mapper.EmpMapper;
import com.xmut.xmutbackend.service.IDeptService;
import com.xmut.xmutbackend.service.IEmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author 邓仲良
 * @since 2025-06-10
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService{

        @Autowired
        private IDeptService deptService; // 如果你有部门表

        @Override
        public EmpStatistics getEmpStatistics() {
            List<Emp> empList = this.list();

            // 性别统计
            long maleCount = empList.stream().filter(e -> e.getGender() != null && e.getGender() == 1).count();
            long femaleCount = empList.stream().filter(e -> e.getGender() != null && e.getGender() == 2).count();

            // 职位统计（数字转中文）
            Map<String, Long> jobStats = empList.stream().collect(Collectors.groupingBy(
                    e -> {
                        if (e.getJob() == null) return "未知";
                        return switch (e.getJob()) {
                            case 1 -> "班主任";
                            case 2 -> "讲师";
                            case 3 -> "学工主管";
                            case 4 -> "教研主管";
                            case 5 -> "咨询师";
                            default -> "其他";
                        };
                    },
                    Collectors.counting()
            ));

            // 部门统计（可以直接用 deptId，也可以查 dept 名）
            Map<String, Long> deptStats = empList.stream().collect(Collectors.groupingBy(
                    e -> {
                        if (e.getDeptId() == null) return "未知";
                        return String.valueOf(e.getDeptId()); // 或通过 DeptService 查名字
                    },
                    Collectors.counting()
            ));

            // 入职年份统计
            Map<String, Long> entryYearStats = empList.stream()
                    .filter(e -> e.getEntrydate() != null)
                    .collect(Collectors.groupingBy(
                            e -> String.valueOf(e.getEntrydate().getYear()),
                            Collectors.counting()
                    ));

            return new EmpStatistics(maleCount, femaleCount, jobStats, deptStats, entryYearStats);
        }
    }



